package com.originskyseed.platform.user.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.originskyseed.platform.user.mapper.*;
import com.originskyseed.platform.user.model.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.user.service.IPlayerRelateChannelLogService;

/**
 * 玩家渠道改绑记录表Service接口实现类（OSI_PLAYER_RELATE_CHANNEL_LOG）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("playerRelateChannelLogService")
public class PlayerRelateChannelLogServiceImpl implements IPlayerRelateChannelLogService {

	@Resource
	private PlayerRelateChannelLogMapper playerRelateChannelLogMapper;

	@Resource
	private PlayerMapper playerMapper;

	@Resource
	private PlayerRelateConsumeMapper playerRelateConsumeMapper;

	@Resource
	private PlayerRelateRechargeMapper playerRelateRechargeMapper;

	@Resource
	private PlayerRelateLoginMapper playerRelateLoginMapper;

	@Resource
	private PlayerRelateViewLiveMapper playerRelateViewLiveMapper;

	@Resource
	private PlayerRelateViewLiveStateMapper playerRelateViewLiveStateMapper;

	@Resource
	private PlayerRelateViewLiveMonthMapper playerRelateViewLiveMonthMapper;

	@Resource
	private PlayerRelateConsumeMonthMapper playerRelateConsumeMonthMapper;

	@Resource
	private PlayerRelateRechargeMonthMapper playerRelateRechargeMonthMapper;



	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.playerRelateChannelLogMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.playerRelateChannelLogMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(PlayerRelateChannelLog record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateChannelLogMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(PlayerRelateChannelLog record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.playerRelateChannelLogMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(PlayerRelateChannelLog record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateChannelLogMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(PlayerRelateChannelLog record) throws Exception {
		record.setUpdateTime(new Date());
		return this.playerRelateChannelLogMapper.updateByPrimaryKeySelective(record);
	}


	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelectiveByTenantCode(String tenantCode,List<PlayerRelateChannelLog> list) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		if(list!=null&&list.size()>0) {
			for(int i=0;i<list.size();i++) {
				PlayerRelateChannelLog playerRelateChannelLog=list.get(i);

				String playerId=playerRelateChannelLog.getPlayerId();
				String channelId=playerRelateChannelLog.getChannelId();
				String channelSubId=playerRelateChannelLog.getChannelSubId();
				String oldChannelId=playerRelateChannelLog.getOldChannelId();
				String oldChannelSubId=playerRelateChannelLog.getOldChannelSubId();
				Date changeChannelTime=playerRelateChannelLog.getChangeChannelTime();

				if(playerId==null||StringUtils.isBlank(playerId)||channelSubId==null||StringUtils.isBlank(channelSubId)||oldChannelSubId==null||StringUtils.isBlank(oldChannelSubId)||changeChannelTime==null)
				{
					playerRelateChannelLog.setIsUpdate(1);
					playerRelateChannelLog.setUpdateTime(new Date());
					return this.playerRelateChannelLogMapper.updateByPrimaryKeySelective(playerRelateChannelLog);
				}
				Map map=new HashMap();
				map.put("playerId",playerId);
				map.put("channelId",channelId);
				map.put("oldChannelId",oldChannelId);
				map.put("channelSubId",channelSubId);
				map.put("oldChannelSubId",oldChannelSubId);
				map.put("changeChannelTime",changeChannelTime);
				//更改业务数据
				//1.玩家表
                this.playerMapper.updateByPlayerIdAndChannelTime(map);
				//2.玩家充值表
				this.playerRelateRechargeMapper.updateByPlayerIdAndChannelTime(map);
				//3.玩家登录表
				this.playerRelateLoginMapper.updateByPlayerIdAndChannelTime(map);
				//4.玩家消费表
				this.playerRelateConsumeMapper.updateByPlayerIdAndChannelTime(map);
				//5.玩家观看直播表
				this.playerRelateViewLiveMapper.updateByPlayerIdAndChannelTime(map);
                //6.玩家观看直播表
				this.playerRelateViewLiveStateMapper.updateByPlayerIdAndChannelTime(map);
				//7.玩家观看直播月度表
                this.playerRelateViewLiveMonthMapper.updateByPlayerIdAndChannelTime(map);
				//8.玩家消费月度表
				this.playerRelateConsumeMonthMapper.updateByPlayerIdAndChannelTime(map);
				//9.玩家充值月度表
				this.playerRelateRechargeMonthMapper.updateByPlayerIdAndChannelTime(map);

				playerRelateChannelLog.setIsUpdate(1);
				playerRelateChannelLog.setUpdateTime(new Date());
				return this.playerRelateChannelLogMapper.updateByPrimaryKeySelective(playerRelateChannelLog);
			}
		}
		return 1;
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public PlayerRelateChannelLog selectByPrimaryKey(String id) throws Exception {
		return this.playerRelateChannelLogMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PlayerRelateChannelLog> selectList(PlayerRelateChannelLog record) throws Exception {
		return this.playerRelateChannelLogMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateChannelLog> selectListByPageByTenantCode(String tenantCode,Map<String, Object> params, BasePage<PlayerRelateChannelLog> page) throws Exception
	{
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return selectListByPage(params,page);
	}
	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<PlayerRelateChannelLog> selectListByPage(Map<String, Object> params, BasePage<PlayerRelateChannelLog> page) throws Exception {
		params.put("page", page);
		List<PlayerRelateChannelLog> list = this.playerRelateChannelLogMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 批量插入
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(List<PlayerRelateChannelLog> list) throws Exception {
		return this.playerRelateChannelLogMapper.insertBatch(list);
	}


	/**
	 * 批量插入
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public int insertBatch(String tenantCode,List<PlayerRelateChannelLog> list) throws Exception
	{
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return insertBatch(list);
	}

	/**
	 * 查询子渠道id和旧子渠道id和时间查询是否存在
	 * 
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<PlayerRelateChannelLog> selectChannelSubIdAndOldChannelSubId(String tenantCode,List<PlayerRelateChannelLog> list) throws Exception {
		if(tenantCode!=null&&StringUtils.isNotBlank(tenantCode))
		{
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		return this.playerRelateChannelLogMapper.selectChannelSubIdAndOldChannelSubId(list);
	}

	/**
	 * 查询子渠道id和旧子渠道id和时间查询是否存在
	 *
	 * @param list
	 * @return
	 * @throws Exception
	 */
	public List<PlayerRelateChannelLog> selectChannelSubIdAndOldChannelSubId(List<PlayerRelateChannelLog> list) throws Exception
	{
		return this.playerRelateChannelLogMapper.selectChannelSubIdAndOldChannelSubId(list);
	}

	/**
	 * 更新玩家改变渠道定时任务
	 * 
	 * @param tenantCode
	 * @throws Exception
	 */
	public void updatePlayerChannel(String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}

		PlayerRelateChannelLog query = new PlayerRelateChannelLog();
		query.setIsUpdate(0);
		List<PlayerRelateChannelLog> playerRelateChannelLogList = this.playerRelateChannelLogMapper.selectList(query);
		for (PlayerRelateChannelLog playerRelateChannelLog : playerRelateChannelLogList) {
			// 更新玩家渠道信息
			Player player = this.playerMapper.selectByPlayerId(playerRelateChannelLog.getPlayerId());
			if (player != null) {
				player.setChannelId(playerRelateChannelLog.getChannelId());
				player.setChannelSubId(playerRelateChannelLog.getChannelSubId());
				this.playerMapper.updateByPrimaryKeySelective(player);
			}

			Map<String, Object> map = new HashMap<>();
			map.put("playerId", playerRelateChannelLog.getPlayerId());
			map.put("channelId", playerRelateChannelLog.getChannelId());
			map.put("channelSubId", playerRelateChannelLog.getChannelSubId());
			map.put("oldChannelId", playerRelateChannelLog.getOldChannelId());
			map.put("oldChannelSubId", playerRelateChannelLog.getOldChannelSubId());
			map.put("changeChannelTime", playerRelateChannelLog.getChangeChannelTime());
			map.put("updateTime", new Date());
			// 更新玩家消费信息
			this.playerRelateConsumeMapper.updateByChannelSubId(map);
			// 更新玩家充值信息
			this.playerRelateRechargeMapper.updateByChannelSubId(map);
			// 更新玩家登录信息
			this.playerRelateLoginMapper.updateByChannelSubId(map);
			// 更新玩家观看直播信息
			this.playerRelateViewLiveMapper.updateByChannelSubId(map);
			playerRelateChannelLog.setIsUpdate(1);
			playerRelateChannelLog.setUpdateTime(new Date());
			this.playerRelateChannelLogMapper.updateByPrimaryKeySelective(playerRelateChannelLog);
		}
	}
}